import java.util.ArrayList;
import java.util.Scanner;

public class Game {

	// var

	ArrayList<Player> players;
	Deck mydeck;
	ArrayList<Card> dealerHand;
	int score;
	String yon;

	// con

	public Game(Deck thedeck, ArrayList<Player> playered) {
		players = new ArrayList<Player>();
		players.addAll(playered);
		mydeck = thedeck;
		dealerHand = new ArrayList<Card>();
		score = 0;
		thedeck.shuffle();
		System.out.println("The deck has been shuffled.....");
		playGame();

	}

	// meth
	// deal first hand
	// player turn
	// repeat for each player
	// play game

	public void playGame() {
		Scanner s = new Scanner(System.in);
		dealFirst();

		System.out.println("Dealer has " + score);

		for (int i = 0; i < players.size(); i++) {
			while (players.get(i).getScore() < 22) {
				players.get(i);

				System.out.println("It's " + players.get(i).named()
						+ "'s turn.\nYou have " + players.get(i).handed()
						+ "\n" + players.get(i).getScore());
				if (players.get(i).getScore() <= 20) {
					System.out.println("Would you like to Hit or Stay?");
					yon = s.next();
					if (yon.equalsIgnoreCase("HIT")) {
						Card yes = mydeck.draw();
						if (yes.getValue() == 1) {
							if ((players.get(i).getScore() + 11) <= 21) {
								yes.ace();
								players.get(i).hit(yes);
							}
						} else {
							players.get(i).hit(yes);
						}

						System.out.println(players.get(i).toString());
					} else if (yon.equalsIgnoreCase("STAY")) {
						break;
					}
				} else if (players.get(i).getScore() >= 22) {
					System.out.println("Busted!");
					break;
				} else if (players.get(i).getScore() == 21) {
					break;
				}
			}
		}
		while (score <= 21 || score >= 17) {
			if (score >= 22) {
				score = 0;
				break;
			} else if (score <= 16) {
				System.out.println("Dealer hits");
				dealerHand.add(mydeck.draw());
				try {
					Thread.currentThread().sleep(2000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				score += mydeck.draw().getValue();
				System.out.println("Dealer has " + score);
			} else if (score >= 17) {
				break;
			}

		}
		for (int i = 0; i < players.size(); i++) {
			if (players.get(i).getScore() >= 22) {
				players.get(i).nope();
			}
		}
		for (int i = 0; i < players.size(); i++) {
			if (players.get(i).getScore() > score) {
				System.out.println(players.get(i).named()
						+ " beat the dealer with " + players.get(i).getScore()
						+ "!");
			} else if (score > players.get(i).getScore()) {
				System.out.println("The dealer beat " + players.get(i).named()
						+ "!");
			} else if (players.get(i).getScore() == score) {
				System.out.println(players.get(i).named()
						+ " tied with the dealer.");
			}
		}

	}

	private void dealFirst() {
		for (int i = 0; i < players.size(); i++) {
			Card yes = mydeck.draw();
			if (yes.getValue() == 1) {
				if ((players.get(i).getScore() + 11) <= 21) {
					yes.ace();
					players.get(i).hit(yes);
				}
			} else {
				players.get(i).hit(yes);
			}
			
			
			
			Card yes1 = mydeck.draw();
			if (yes1.getValue() == 1) {
				if ((players.get(i).getScore() + 11) <= 21) {
					yes1.ace();
					players.get(i).hit(yes1);
				}
			} else {
				players.get(i).hit(yes1);
			}
		}

		dealerHand.add(mydeck.draw());
		score += mydeck.draw().getValue();
		dealerHand.add(mydeck.draw());
		score += mydeck.draw().getValue();
	}

	private void getHand(Card asdf) {
		dealerHand = new ArrayList<Card>();
		dealerHand.add(asdf);
	}

	private void hit(Card qwe) {

	}

}
